<html>
<head>
  <title>Nearest Neighbors</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="947"/>
<h1>Nearest Neighbors</h1>

<div>
<span><div><div><a href="http://scikit-learn.org/stable/modules/classes.html#module-sklearn.neighbors" title="sklearn.neighbors">        sklearn.neighbors</a> provides functionality for unsupervised and supervised neighbors-based learning methods. <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">Unsupervised nearest neighbors is the foundation of many other learning methods, notably manifold learning and spectral clustering. Supervised neighbors-based learning comes in two flavors:</span> <a href="http://scikit-learn.org/stable/modules/neighbors.html#classification" style="font-size: 12pt; font-weight: bold; color: rgb(28, 51, 135);">classification</a> <span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">for data with discrete labels, and</span> <a href="http://scikit-learn.org/stable/modules/neighbors.html#regression" style="font-size: 12pt; font-weight: bold; color: rgb(28, 51, 135);">regression</a> <span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">for data with continuous labels.</span></font></div><div><br/></div><div>principle:</div><div>        to find a predefined number of training samples closest in distance to the new point, and predict the label from these. The number of samples can be a user-defined constant (k-nearest neighbor learning), or vary based on the local density of points (radius-based neighbor learning).</div><div><br/></div><h2><span style="color: rgb(28, 51, 135); font-size: 18pt; font-weight: normal;">Unsupervised Nearest Neighbors</span></h2><div><span>    <span>    </span></span> It acts as a uniform interface to three different nearest neighbors algorithms: <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.BallTree.html#sklearn.neighbors.BallTree" title="sklearn.neighbors.BallTree">BallTree</a>, <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KDTree.html#sklearn.neighbors.KDTree" title="sklearn.neighbors.KDTree">KDTree</a>, and a brute-force algorithm based on routines in <a href="http://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics.pairwise" title="sklearn.metrics.pairwise">sklearn.metrics.pairwise</a>.</div><div><span>    <span>     choice of neighbors search algorithm is controlled through the keyword 'algorithm', which must be one of ['auto', 'ball_tree', 'kd_tree', 'brute'].</span></span><br/></div><h3>Finding the Nearest Neighbors</h3><div><span>    <span>    </span></span>For the simple task of finding the nearest neighbors between two sets of data, the unsupervised algorithms within <a href="http://scikit-learn.org/stable/modules/classes.html#module-sklearn.neighbors" title="sklearn.neighbors">sklearn.neighbors</a> can be used</div><h3>KDTree and BallTree Classes</h3><div><span>    <span>    </span></span>Alternatively, one can use the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KDTree.html#sklearn.neighbors.KDTree" title="sklearn.neighbors.KDTree">KDTree</a> or <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.BallTree.html#sklearn.neighbors.BallTree" title="sklearn.neighbors.BallTree">BallTree</a> classes directly to find nearest neighbors. This is the functionality wrapped by the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html#sklearn.neighbors.NearestNeighbors" title="sklearn.neighbors.NearestNeighbors">NearestNeighbors</a> class used above</div><div><br/></div><h2><font style="font-size: 18pt; font-weight: normal; color: rgb(28, 51, 135);">Nearest Neighbors Classification</font></h2><div><span>    <span>    </span></span>Neighbors-based classification is a type of <span style="font-style: italic;">instance-based learning</span> or <span style="font-style: italic;">non-generalizing learning</span>: it does not attempt to construct a general internal model, but <font style="color: rgb(173, 0, 0); font-size: 12pt;"><b>simply stores instances of the training data</b></font>.</div><div><span>    <span>    scikit-learn implements two different nearest neighbors classifiers: <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier" title="sklearn.neighbors.KNeighborsClassifier">KNeighborsClassifier</a> implements learning based on the <img src="Nearest Neighbors_files/Image.png" type="image/png" data-filename="Image.png"/> nearest neighbors of each query point, where <img src="Nearest Neighbors_files/Image [1].png" type="image/png" data-filename="Image.png"/> is an integer value specified by the user. <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsClassifier.html#sklearn.neighbors.RadiusNeighborsClassifier" title="sklearn.neighbors.RadiusNeighborsClassifier">RadiusNeighborsClassifier</a> implements learning based on the number of neighbors within a fixed radius <img src="Nearest Neighbors_files/Image [2].png" type="image/png" data-filename="Image.png"/> of each training point, where <img src="Nearest Neighbors_files/Image [3].png" type="image/png" data-filename="Image.png"/> is a floating-point value specified by the user.</span></span><br/></div><div><br/></div><h2><font color="#1C3387" style="font-size: 18pt; font-weight: normal;">Nearest Neighbors Regression</font></h2><div><span>    <span>    scikit-learn implements two different neighbors regressors: <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html#sklearn.neighbors.KNeighborsRegressor" title="sklearn.neighbors.KNeighborsRegressor">KNeighborsRegressor</a> implements learning based on the <img src="Nearest Neighbors_files/Image [4].png" type="image/png" data-filename="Image.png"/> nearest neighbors of each query point, where <img src="Nearest Neighbors_files/Image [5].png" type="image/png" data-filename="Image.png"/> is an integer value specified by the user. <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsRegressor.html#sklearn.neighbors.RadiusNeighborsRegressor" title="sklearn.neighbors.RadiusNeighborsRegressor">RadiusNeighborsRegressor</a> implements learning based on the neighbors within a fixed radius <img src="Nearest Neighbors_files/Image [6].png" type="image/png" data-filename="Image.png"/> of the query point, where <img src="Nearest Neighbors_files/Image [7].png" type="image/png" data-filename="Image.png"/> is a floating-point value specified by the user.</span></span><br/></div><div><br/></div><h2><font style="font-size: 18pt; color: rgb(28, 51, 135); font-weight: normal;">Nearest Neighbor Algorithms</font></h2><h3>Brute Force</h3><h3>K-D Tree</h3><h3>Ball Tree</h3><h3>Choice of Nearest Neighbors Algorithm</h3><h3>Effect of leaf_size</h3><div><br/></div><h2><font color="#1C3387" style="font-size: 18pt; font-weight: normal;">Nearest Centroid Classifier</font></h2><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div><div><br/></div></span>
</div></body></html> 